package br.com.salao.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.salao.bean.DiaSemana;
import br.com.salao.dao.DiaSemanaDao;
import br.com.salao.util.JpaUtil;

public class DiaSemanaDaoImpl implements DiaSemanaDao {	

	private final GenericDao<DiaSemana> dao;

	public DiaSemanaDaoImpl(EntityManager em) {
		dao = new GenericDao<DiaSemana>(em, DiaSemana.class);
	}

	public DiaSemana inserir(DiaSemana diaSemana) {
		return dao.salvar(diaSemana);		
	}

	public List<DiaSemana> listar(){		
		return dao.lista();
	}		

	public DiaSemana update(DiaSemana diaSemana){
		return dao.update(diaSemana);
	}
	
	public DiaSemana buscarDiaSemanaPorIdEstabelecimentos(int idEstabelecimento){
		
		EntityManager em = JpaUtil.getEntityManager();

		DiaSemana diaSemana = null;

		try {		
			Query query = em.createQuery("from DiaSemana e where e.estabelecimento.id like :idEstabelecimento");
			query.setParameter("idEstabelecimento", idEstabelecimento);
			diaSemana = (DiaSemana) query.getSingleResult();	
			
		} catch (Exception e) {
			diaSemana = null;
			e.printStackTrace();
			
		} finally{
			if(em != null){
				em.close();
			}
		}
		return diaSemana;			
	}	

}
